package com.mark.org.controller;

import com.mark.basic.annotation.PreAuthorize;
import com.mark.basic.util.AjaxResult;
import com.mark.basic.util.PageList;
import com.mark.org.domain.Department;
import com.mark.org.query.DepartmentQuery;
import com.mark.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class    DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @GetMapping("/{id}")
    /**
     * 查询单个数据
     */
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.listById(id);
    }
    @GetMapping
    /**
     * 查询所有数据
     */
    public List<Department> queryAll(){
        return departmentService.listAll();
    }
    @DeleteMapping("/{id}")
    /**
     * 删除数据
     */
    @PreAuthorize(name = "删除数据",value = "department:deleteById")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
    @PutMapping
    /**
     * 添加或修改数据
     */
    public AjaxResult addOrUpdate(@RequestBody Department department){
        System.err.println(department.getId());
        if (department.getId() == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me();
    }
    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        PageList<Department> aa = departmentService.queryPage(query);
        return aa;
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
//            System.out.println(ids);
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
    /**
     * 查询无限极部门树
     */
    @GetMapping("/tree")
    public List<Department> queryTree(){
       return departmentService.queryTree();
    }
    
}